package cn.afterturn.easypoi.controller;

import cn.afterturn.easypoi.entity.ExcelPojo;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

//触发数据核对
@RestController
@RequestMapping("/excel")
public class compareController {

    @RequestMapping("/compare")
    public void compare(HttpServletRequest request, HttpServletResponse response) throws IOException {


        String realPath = request.getSession().getServletContext().getRealPath("/upload");

        File file1 = new File(realPath+"/a.xlsx");
        File file2 = new File(realPath+"/b.xlsx");

        ImportParams params = new ImportParams();
        params.setTitleRows(0);
        params.setKeyIndex(0);

        List<ExcelPojo> EP1 = ExcelImportUtil.importExcel(file1, ExcelPojo.class, params);
        List<ExcelPojo> EP2 = ExcelImportUtil.importExcel(file2, ExcelPojo.class, params);
        List<ExcelPojo> EP3 = new ArrayList<>();

        //按条件筛选数据
        for (int i = 0; i < EP1.size(); i++) {
            ExcelPojo excelPojo = EP1.get(i);
            //得到第一张表中的第一个日期
            String date = excelPojo.getDate();
            //拿这个日期去第二张表中找相同日期的 (遍历第二张表)
            for (int j = 0; j < EP2.size(); j++) {
                //如果日期一样,再去比较店铺名
                if (date.equals(EP2.get(j).getDate())){
                    //如果店铺名一样,再去比较 单量是否相等
                    if (excelPojo.getName().equals(EP2.get(j).getName())){
                        //比较单量是否相等
                        if (excelPojo.getNum()==EP2.get(j).getNum()){
                            //比较总本金是否相等
                            if (excelPojo.getMoney()==EP2.get(j).getMoney()){
                                //如果以上条件都满足,那么把这个符合要求的数据存入List中
                                EP3.add(excelPojo);
                            }
                        }
                    }


                }
            }

        }


        //生成表
        ExportParams exportParams = new ExportParams();
        exportParams.setTitle("c");
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, ExcelPojo.class, EP3);
        //FileOutputStream fos = new FileOutputStream("c:/"+ UUID.randomUUID()+".xls");
        //workbook.write(fos);
        response.setContentType("application/octet-stream;charset=UTF-8");
        response.setHeader("Content-disposition", "attachment;filename="+UUID.randomUUID()+".xls" );
        OutputStream os = response.getOutputStream();
        workbook.write(os);
        os.flush();
        os.close();
        //删除服务器文件
        file1.delete();
        file2.delete();

    }


}
